//
//  MineViewController.m
//  LineMoney
//
//  Created by 范琦 on 16/7/8.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "MineViewController.h"
#import "MineTopView.h"
#import "BigTableViewCell.h"
#import "MineTableViewCell.h"
#import "CompleteViewController.h"
#import "PendingViewController.h"
#import "BillViewController.h"
#import "MyBidViewController.h"
#import "MyTenderViewController.h"
#import "MyTeamViewController.h"
#import "MyDetailViewController.h"
#import "MyAccountViewController.h"
#import "MyTakingViewController.h"
#import "ExitView.h"
#import "ChangeLanguageView.h"

@interface MineViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    
}
@property (nonatomic, strong) MineTopView *topView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) RACDisposable *disposable;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) ExitView *ExitView;
@property (nonatomic, strong) ChangeLanguageView *ChangeLanguageView;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的";
    
    [self creatTopView];
    [self creatTableView];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = NO    ;

}

- (void)creatTopView
{
    self.topView = [[[NSBundle mainBundle] loadNibNamed:@"MineTopView" owner:self options:nil] lastObject];
    [[self.topView.WriteButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        MyDetailViewController *myDetail = [[MyDetailViewController alloc] init];
        [self.navigationController pushViewController:myDetail animated:YES];
    }];
    _topView.frame = CGRectMake(0, 0, KSCREENWIDTH, 176);
    _topView.backgroundColor = BACKGROUNDCOLOR;
    [self.view addSubview:_topView];
    
}

- (void)creatTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, _topView.frame.size.height, KSCREENWIDTH, KSCREENHEIGHT  - _topView.bounds.size.height - 30) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"BigTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [_tableView registerNib:[UINib nibWithNibName:@"MineTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell1"];
    [self.view addSubview:_tableView];
    _tableView.sectionFooterHeight = 0;
    

}

#pragma mark - TableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1 || section == 2) {
        return 2;
    } else {
        return 1;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 6;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.1;
    } else {
    return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 105;
    } else {
        return 44;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BigTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    MineTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
    cell1.tintColor = [UIColor colorWithRed:51 / 255.0 green:51 / 255.0 blue:51 / 255.0 alpha:1];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell1 setSelectionStyle:UITableViewCellSelectionStyleNone];// 去掉点击之后的阴影

    [[[cell.CompleteButton rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(id x) {
        CompleteViewController *complete = [[CompleteViewController alloc] init];
        [self.navigationController pushViewController:complete animated:YES];
    }];
    [[[cell.PendingButton rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(id x) {
        PendingViewController *pending = [[PendingViewController alloc] init];
        [self.navigationController pushViewController:pending animated:YES];
    }];
    [[[cell.BillButton rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(id x) {
        BillViewController *bill = [[BillViewController alloc] init];
        [self.navigationController pushViewController:bill animated:YES];
    }];
    
    
    if (indexPath.section == 0) {
        
        return cell;
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell1.TableviewIcon.image = [UIImage imageNamed:@"MyBid"];
            cell1.TableviewTitle.text = @"我的投标";
            cell1.tintColor = [UIColor colorWithRed:51 / 255.0 green:51 / 255.0 blue:51 / 255.0 alpha:1];
        } else {
            cell1.TableviewIcon.image = [UIImage imageNamed:@"MyTender"];
            cell1.TableviewTitle.text = @"我的招标";
        }
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
                cell1.TableviewIcon.image = [UIImage imageNamed:@"MyTeam"];
                cell1.TableviewTitle.text = @"我的团队";
            } else {
                cell1.TableviewIcon.image = [UIImage imageNamed:@"MyProfit"];
                cell1.TableviewTitle.text = @"我的利润";
            }
        } else if (indexPath.section == 3) {
            cell1.TableviewIcon.image = [UIImage imageNamed:@"MyAccount"];
            cell1.TableviewTitle.text = @"我的账户";
        } else if (indexPath.section == 4) {
            cell1.TableviewIcon.image = [UIImage imageNamed:@"Language"];
            cell1.TableviewTitle.text = @"语言";
        } else {
            cell1.TableviewIcon.image = [UIImage imageNamed:@"Exit"];
            cell1.TableviewTitle.text = @"退出";
        }
        return cell1;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            MyBidViewController *MyBid = [[MyBidViewController alloc] init];
            [self.navigationController pushViewController:MyBid animated:YES];
        } else {
            MyTenderViewController *myTender = [[MyTenderViewController alloc] init];
            [self.navigationController pushViewController:myTender animated:YES];
        }
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            MyTeamViewController *myTeam = [[MyTeamViewController alloc] init];
            [self.navigationController pushViewController:myTeam animated:YES];
        } else {
            MyTakingViewController *myTaing = [[MyTakingViewController alloc] init];
            [self.navigationController pushViewController:myTaing animated:YES];
        }
    } else if (indexPath.section == 3) {
        MyAccountViewController *MyAccount = [[MyAccountViewController alloc] init];
        [self.navigationController pushViewController:MyAccount animated:YES];
    } else if (indexPath.section == 4) {
        [self creatChangeLanguageView];
    } else {
        [self creatExitView];
    }
 
    
}

- (void)creatChangeLanguageView
{
    self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT - 64 + 20)];
    _backView.backgroundColor = [UIColor blackColor];
    _backView.alpha = 0.5;
    self.ChangeLanguageView = [[[NSBundle mainBundle] loadNibNamed:@"ChangeLanguageView" owner:nil options:nil] lastObject];
    
    _ChangeLanguageView.frame = CGRectMake(55 * KSCREENWIDTH / 375, 182 * KSCREENHEIGHT / 667, 265 * KSCREENWIDTH / 375, 158 * KSCREENHEIGHT / 667);
    [[_ChangeLanguageView.ChineseButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [_backView removeFromSuperview];
        [_ChangeLanguageView removeFromSuperview];
    }];
    [[_ChangeLanguageView.EnglishButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [_backView removeFromSuperview];
        [_ChangeLanguageView removeFromSuperview];
    }];
    [self.view addSubview:_backView];
    [self.view addSubview:_ChangeLanguageView];
    
}


- (void)creatExitView
{
    self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT - 64 + 20)];
    _backView.backgroundColor = [UIColor blackColor];
    _backView.alpha = 0.5;
    self.ExitView = [[[NSBundle mainBundle] loadNibNamed:@"ExitView" owner:nil options:nil] lastObject];
    
    _ExitView.frame = CGRectMake(55 * KSCREENWIDTH / 375, 182 * KSCREENHEIGHT / 667, 265 * KSCREENWIDTH / 375, 158 * KSCREENHEIGHT / 667);
    _ExitView.CancelButton.layer.cornerRadius = 20;
    _ExitView.CancelButton.layer.masksToBounds = YES;
    _ExitView.YesButton.layer.cornerRadius = 20;
    _ExitView.YesButton.layer.masksToBounds = YES;
    [[_ExitView.CancelButton rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
        [_backView removeFromSuperview];
        [_ExitView removeFromSuperview];
    }];
    [[_ExitView.YesButton rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
        [_backView removeFromSuperview];
        [_ExitView removeFromSuperview];
    }];
    [self.view addSubview:_backView];
    [self.view addSubview:_ExitView];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
